<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Ringo Chan <334258471@qq.com> <http://www.jxlnxx.com/>
// +----------------------------------------------------------------------

namespace Api\Controller;
use Com\Message;
/**
 * API公共控制器
 * 主要获取公共数据
 */
class PublicController extends ApiController {
    
    /**
     * 获取验证码
     *
     * @author Ringo Chan<334258471@qq.com>
     */
    public function verify(){
        params_exist(array('user', 'type'));
         
        $username = I('post.user');
        $type = I('post.type');
         
        empty($username) && response(-1, '', '手机号码不能为空');
        empty($type) && response(-1, '', '类型不能为空');
        !preg_match('/1[34578]{1}\d{9}$/', $username) && response(-2, '', '请填入正确的手机号码');
        !in_array($type, array('register', 'forgotten', 'modifymobile')) && response(-2, '', '类型不正确');
        
        if ($type == 'register') {
            $Ucenter_member = M('Ucenter_member');
             
            $info = $Ucenter_member->where(array('username'=>$username))->find();
            
            $info && response(-3, '', '账户已存在');
        }
         
        $code = getRandCode(6);
         
        $data = array(
            'username' => $username,
            'type' => $type,
            'code' => $code,
            'create_time' => NOW_TIME,
            'deadline' => NOW_TIME + 1800,
            'status' => 1
        );
         
        $Message_verify = M('Message_verify');
         
        $Message_verify->create($data);
        $Message_verify->add();
        $juhe=M('config_private','cmf_')->find();
        $key=$juhe['juhe_appkey'];
        $tplid=$juhe['juhe_tplid'];
        Message::setConfig('key',$key);
        Message::send($username,'#code#='.$code,$tplid);
        response(1, array('verify'=>$code));
    }
    
    /**
     * 获取省份列表
     *
     * @author Ringo Chan<334258471@qq.com>
     */
    public function getProvinces(){
         $list = getProvinces();
         
         $list ? response(1, $list) : response(-1, '', '没有找到匹配对象');
    }
    
    /**
     * 获取对应省份的城市列表
     * 
     * @param string province_id 省份标识
     * @author Ringo Chan<334258471@qq.com>
     */
    public function getCities(){
        params_exist(array('province_id'));
        
        $province_id = I('post.province_id');
        
        $list = getCities($province_id);
         
        $list ? response(1, $list) : response(-1, '', '没有找到匹配对象');
    }
    
    /**
     * 获取对应城市的区县列表
     * 
     * @param string city_id 城市标识
     * @author Ringo Chan<334258471@qq.com>
     */
    public function getAreas(){
        params_exist(array('city_id'));
        
        $city_id = I('post.city_id');
        
        $list = getAreas($city_id);
         
        $list ? response(1, $list) : response(-1, '', '没有找到匹配对象');
    }
    
    /**
     * 获取抢购会时间表
     *
     * @author Ringo Chan <334258471@qq.com>
     */
    public function getSpecialPanic(){
        $list = getSpecialPanic();
        
        $list ? response(1, $list) : response(-1, '', '没有找到匹配对象');
    }
    
    /**
     * 获取状态列表
     *
     * @author Ringo Chan <334258471@qq.com>
     */
    public function getStatus(){
        $model = I('post.model');
        $id = I('post.id', null);
        $status = getStatus($model, $id);
    
        response(1, $status);
    }
    
    /**
     *广告
     *@param string identifier
     */
    public function adlist(){
        $M_ad=M('ad');
        $M_ad_position=M('ad_position');
        $identifier=I('post.identifier');
        
        $map=array();
        if($identifier){
            $map['identifier']=$identifier;
        }
       
        $plist=$M_ad_position->where($map)->field('id,title,identifier')->select();
        
        foreach($plist as &$item){
            $ad=$M_ad->where(array('position_id'=>$item['id']))
            ->order('sort DESC')->field('cover_id,title,link,product_id')->select();
            foreach($ad as &$_child){
                $_child['images']=get_url($_child['cover_id']);
            }
            $item['total']=count($ad);
            $item['ad_list']=$ad;
        }
        
        response(1,$plist);
    }
    /**
     * 聚真宝地址
     */
    public function jzbAddress(){
        
         C('JZB_COMPANY_ADDRESS')  ? response(1,C('JZB_COMPANY_ADDRESS')) : response(-2);
    }
    
    /**
     * 物流公司
     */
    public function shippingList(){
        $M_shipping=M('shipping');
        
        $list=$M_shipping->select();
        
        $list ? response(1,$list) : response(-2);
    }
    
    /**
     * 搜索
     * @param int page
     * @param int pagesize
     * @param int category_id
     * @param int keyword
     * @param int type
     */
    public function search(){
        params_exist(array('keyword'));        
        $category_id = I('post.category_id'); // 商品分类编号
        $keyword=I('post.keyword');
        $type=I('post.type');
        $page = I('post.page', 1); // 页码
        $page <=0 && $page=1;
        $pagesize = I('post.pagesize', 10); // 分页显示条数
        $pagesize <=0 && $pagesize=10;
        $category_id = I('post.category_id'); // 商品分类编号
        $start = ($page - 1) * $pagesize;
        
        empty($keyword) && response(-1,'','关键词不能为空');
        !preg_match('/^.{1,25}$/', $keyword) && response(-1,'','关键词在1-25字之间');
        $product = M('product');
        $map=array();
        $map['type']=array('in','putong,paipin,xianzhi,dingzhi');
        $map['status'] = 1;
        $map['is_show'] = 1;
        $type && $map['type']=$type;
        $map['title']=array('like','%'.$keyword.'%');
        !(empty($category_id) || !is_numeric($category_id)) && $map['category_id'] = $category_id;        
       
        $total=$product->where($map)->count();
        $list = $product->limit($start,$pagesize)->where($map)->field('id,title,type,price,quantity,view,shop_id')->order('id ASC')->select();
        
        /* 增加附加属性值：image、discount */
        $M_product_image = M('product_image');
        
        foreach ($list as&$item) {
            $image_info = $M_product_image->where(array('product_id'=>$item['id']))->order('id ASC')->find();
        
            $item['image'] = get_url($image_info['cover_id'], 'path');
        }
        
        empty($list) && response(-2);
        
        $result = array(
            'total' => $total,
            'result' => $list
        );
        
        response(1, $result);        
    }
    
    /**
     * app版本
     * @param string os
     */
    public function appVersion(){
        $M_app_version = M('app_version');
        
        $map=array();
        $os=I('os');
        if($os){
            $map['os']=$os;
        }
        
        $list=$M_app_version->where($map)->select();
        $list ? response(1,$list) : response(-2);
    }
    
    /**
     * app版本
     * @param int $id
     */
    public function versionDetail(){
        params_exist(array('id'));
        $M_app_version = M('app_version');
        $id=I('id');
        $info=$M_app_version->find($id);
       
        $info ? response(1,$info) : response(-2);
    }
    
    public function services(){
        // $phone=C('CONFIG_PHONE');
        // $hx_account=C('HX_ONLINE_ACCOUNT');
        //$hx_account=explode(',', $hx_account);
        $phone = M('service')->getField('phone');

        $zbuid= M('users','cmf_')->where('user_login='.$phone)->getField('id');
        $result['zbuid'] = $zbuid;
        $result['phone'] = $phone;
        $result['hx_accoun']=$phone;

        response(1,$result);
    }
}
?>